import Vue from "vue";
import VueRouter from "vue-router";
import store from '../store/index.js'
import request from '../util/request.js'

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
	redirect:"/admin/home",
  },
  {
    path: "/login",
    name: "login",
    component: () => import( "../views/LoginView.vue"),
  },
  {
    path: "/admin",
    name: "admin",
	redirect:"/admin/home",
    component: () => import( "../components/AdminTpl.vue"),
	children:[
		{
		  path: "home",
		  name: "adminhome",
		  component: () => import( "../views/admin/HomeView.vue"),
		},
	]
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to,from,next) => {
	console.log("是否登录",store.getters.isLogin)
	// 用户已经登录了但是没有用户信息的时候
	// if(store.getters.isLogin && !store.getters.hasInfo){
	// 	request.get('account/detail').then(res => {
	// 		console.log("用户信息",res)
	// 		store.commit('setUsername',res.username)
	// 	}).catch(error => {
	// 		console.log("用户信息获取失败",error)
	// 		store.commit('setToken','')
	// 		store.commit('setUsername','')
	// 	})
	// }
	// 判断有么有登录 登录跳转判断
	if(!store.getters.isLogin){
		// 去的是否是登录页面
		if(to.path === '/login'){
			// 直接进去
			next()
		}else{
			// 跳转到登录页
			next('/login')
		}
	}else{
		// 去的是否是登录页面
		if(to.path === '/login'){
			// 直接进去
			next('/admin/home')
		}else{
			// 跳转到登录页
			next()
		}
	}
})

export default router;
